//package com.yueyang.sojourn.server.controller.wx;
//
//import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
//import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
//import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
//import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
//import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingQueryResult;
//import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingReceiverResult;
//import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingResult;
//import com.github.binarywang.wxpay.bean.profitsharing.ProfitSharingReturnResult;
//import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
//import com.github.binarywang.wxpay.bean.result.*;
//import com.github.binarywang.wxpay.constant.WxPayConstants;
//import com.github.binarywang.wxpay.exception.WxPayException;
//import com.github.binarywang.wxpay.service.ProfitSharingService;
//import com.github.binarywang.wxpay.service.WxPayService;
//import com.yueyang.sojourn.server.common.LoginNeedless;
//import com.yueyang.sojourn.server.controller.BaseController;
//import com.yueyang.sojourn.server.controller.model.JsonResult;
//import com.yueyang.sojourn.server.model.po.SojournOrder;
//import com.yueyang.sojourn.server.service.sojourn.merch.SojournMerchService;
//import com.yueyang.sojourn.server.service.sojourn.order.SojournOrderService;
//import com.yueyang.sojourn.server.service.sojourn.pay.PayService;
//import com.yueyang.sojourn.server.service.sojourn.transaction.SojournTransactionService;
//import com.yueyang.sojourn.server.utils.IpUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import org.apache.commons.io.IOUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//
///**
// * @author Andy.yang
// * @version v1
// * @summary
// * @Copyright (c) 2019, yueyang Group All Rights Reserved.
// * @since 2020/3/6
// */
//@RestController
//@RequestMapping("/api/wx/order")
//@Api(tags = "(小程序)订单 API")
//public class ApiPayController extends BaseController {
//    @Autowired
//    private WxPayService wxPayService;
//    @Autowired
//    private ProfitSharingService profitSharingService;
//    @Autowired
//    private SojournOrderService orderService;
//    @Autowired
//    private SojournMerchService merchService;
//    @Autowired
//    private SojournTransactionService transactionService;
//    @Autowired
//    private PayService payService;
//
//
//
//    @ApiOperation(value = "微信支付预付定单")
//    @GetMapping("/prepay")
//    public JsonResult prepay(@ApiParam(value = "订单id")
//                                 @RequestParam(value = "orderId") Long orderId,
////                             @ApiParam(value = "app类型：1-小程序；2-H5") @RequestParam(value = "appType")Integer appType,
//                             HttpServletRequest request) {
////        //我自己的id
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs" ;
////        Long userId = getLoginUserId();
////
////        SojournOrder order = orderService.findById(orderId);
////        if(order == null){
////            return fail("订单不存在");
////        }
////        SojournMerch merch = merchService.selectByPrimaryKey(order.getMerchId());
////        String ip = IpUtil.getIpAddr(request);
////        String prepayId = payService.prepay(order,openId,ip);
////        if(prepayId == null){
////            return fail("订单预支付失败");
////
////        }
//
//        WxPayMpOrderResult result = payService.prepay(orderId.toString(),openId,IpUtil.getIpAddr(request),true);
//        JsonResult rs = ok();
//        rs.setData(result);
//        return rs;
//    }
//
//
//    /**
//     * 微信付款成功或失败回调接口
//     * <p>
//     *  TODO
//     *  注意，这里pay-notify是示例地址，建议开发者应该设立一个隐蔽的回调地址
//     *
//     * @param request 请求内容
//     * @return 操作结果
//     */
//    @LoginNeedless
//    @PostMapping("pay-notify")
//    public Object payNotify(HttpServletRequest request) {
//        String xmlResult = null;
//        try {
//            xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
//        } catch (IOException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//
//        WxPayOrderNotifyResult result = null;
//        try {
//            result = wxPayService.parseOrderNotifyResult(xmlResult);
//
//            if(!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())){
////                logger.error(xmlResult);
//                throw new WxPayException("微信通知支付失败！");
//            }
//            if(!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())){
////                logger.error(xmlResult);
//                throw new WxPayException("微信通知支付失败！");
//            }
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//
////        logger.info("处理腾讯支付平台的订单支付");
////        logger.info(result);
//
//        String orderSn = result.getOutTradeNo();
//        String payId = result.getTransactionId();
//
//        // 分转化成元
//        String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
//        System.out.println("totalFee=" + totalFee);
//        return WxPayNotifyResponse.success("处理成功!");
//    }
//
//
//
//    @ApiOperation(value = "关闭wx订单")
//    @LoginNeedless
//    @GetMapping("/closeOrder")
//    public JsonResult closeOrder(@ApiParam(value = "订单id") @RequestParam Long orderId, HttpServletRequest request) {
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs";
//        try {
//            SojournOrder order = orderService.findById(orderId);
//            WxPayOrderCloseResult result = wxPayService.closeOrder(order.getSn());
//            JsonResult ok = ok();
//            ok.setData(result);
//            return ok;
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//    }
//
//    /**
//     * 微信退款成功或失败回调接口
//     * <p>
//     *  TODO
//     *  注意，这里pay-notify是示例地址，建议开发者应该设立一个隐蔽的回调地址
//     *
//     * @param request 请求内容
//     * @return 操作结果
//     */
//    @LoginNeedless
//    @PostMapping("refund-notify")
//    public Object refundNotify(HttpServletRequest request) {
//        String xmlResult = null;
//        try {
//            xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
//        } catch (IOException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//
//        WxPayRefundNotifyResult result = null;
//        try {
//            result = wxPayService.parseRefundNotifyResult(xmlResult);
//
//            if(!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())){
////                logger.error(xmlResult);
//                throw new WxPayException("微信通知退款失败！");
//            }
//            if(!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())){
////                logger.error(xmlResult);
//                throw new WxPayException("微信通知退款失败！");
//            }
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//
////        logger.info("处理腾讯支付平台的订单支付");
////        logger.info(result);
//
//        String returnMsg = result.getReturnMsg();
//        return WxPayNotifyResponse.success("处理成功!");
//    }
//
//    @ApiOperation(value = "查询wx订单状态")
//    @LoginNeedless
//    @GetMapping("/queryWxOrder")
//    public JsonResult queryWxOrder(@ApiParam(value = "订单id") @RequestParam Long orderId, HttpServletRequest request) {
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs";
//        try {
//            SojournOrder order = orderService.findById(orderId);
//            WxPayOrderQueryResult result = wxPayService.queryOrder(null,order.getSn());
//            JsonResult ok = ok();
//            ok.setData(result);
//            return ok;
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "申请退款")
//    @LoginNeedless
//    @GetMapping("/refund")
//    public JsonResult refund(@ApiParam(value = "订单Sn") @RequestParam String orderSn, HttpServletRequest request) {
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs";
//        try {
////            SojournOrder order = orderService.findById(orderId);
//            WxPayRefundRequest wxRequest = WxPayRefundRequest.newBuilder().build();
//            wxRequest.setTotalFee(10);
//            wxRequest.setOutTradeNo(orderSn);
//            wxRequest.setRefundFee(10);
//            wxRequest.setOutRefundNo("rf" + orderSn);
//            WxPayRefundResult result = wxPayService.refund(wxRequest);
//            JsonResult ok = ok();
//            ok.setData(result);
//            return ok;
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "查询退款")
//    @LoginNeedless
//    @GetMapping("/refundQuery")
//    public JsonResult refundQuery(@ApiParam(value = "订单id") @RequestParam Long orderId, HttpServletRequest request) {
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs";
//        try {
//            String myRfId = "rf" + orderId.toString();
//            WxPayRefundQueryResult result = wxPayService.refundQuery(null,null,myRfId,null);
//            JsonResult ok = ok();
//            ok.setData(result);
//            return ok;
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//    }
//
//    @ApiOperation(value = "下载对账单")
//    @LoginNeedless
//    @GetMapping("/downloadbill")
//    public JsonResult downloadbill(@ApiParam(value = "账单日期") @RequestParam String date, HttpServletRequest request) {
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs";
//        try {
//            WxPayBillResult result = wxPayService.downloadBill(date, WxPayConstants.BillType.ALL,null,null);
//            JsonResult ok = ok();
//            ok.setData(result);
//            return ok;
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//    }
//
//    @LoginNeedless
//    @ApiOperation(value = "下载资金账单")
//    @GetMapping("/downloadFundFlow")
//    public JsonResult downloadfundflow(@ApiParam(value = "账单日期") @RequestParam String date, HttpServletRequest request) {
//        String openId = "o7S1I4xYpbj7jzAzjqwwH__z7GXs";
//        try {
//            WxPayFundFlowResult result = wxPayService.downloadFundFlow(date, WxPayConstants.AccountType.BASIC,null);
//            JsonResult ok = ok();
//            ok.setData(result);
//            return ok;
//        } catch (WxPayException e) {
//            e.printStackTrace();
//            return fail(e.getMessage());
//        }
//    }
//
//
//    //-------------------------------- 分账
//
//    @LoginNeedless
//    @ApiOperation(value = "增加分账方")
//    @GetMapping("/addReceiver")
//    public JsonResult addReceiver(@ApiParam(value = "订单id") @RequestParam Long merchantId,
//                             HttpServletRequest request) throws WxPayException {
//        ProfitSharingReceiverResult receiverResult = payService.addWxShareReceiver(merchantId.toString());
//        JsonResult rs = ok();
//        rs.setData(receiverResult);
//        return rs;
//    }
//
//
//    @LoginNeedless
//    @ApiOperation(value = "单次分账")
//    @GetMapping("/share")
//    public JsonResult shareOne(@ApiParam(value = "订单id") @RequestParam String transId,
//                            @ApiParam(value = "app类型：1-小程序；2-H5") @RequestParam Integer appType,
//                            HttpServletRequest request) throws WxPayException {
//        ProfitSharingResult result = payService.shareOne(transId.toString());
//        JsonResult rs = ok();
//        rs.setData(result);
//        return rs;
//    }
//
//
//    @LoginNeedless
//    @ApiOperation(value = "完结分账")
//    @GetMapping("/finsh")
//    public JsonResult finsh(@ApiParam(value = "订单id") @RequestParam Long transId,
//                                  @ApiParam(value = "app类型：1-小程序；2-H5") @RequestParam Integer appType,
//                                  HttpServletRequest request) throws WxPayException {
//        ProfitSharingResult result = payService.submitShare(transId.toString());
//        JsonResult rs = ok();
//        rs.setData(result);
//        return rs;
//    }
//
//    @LoginNeedless
//    @ApiOperation(value = "完结分账查询")
//    @GetMapping("/finshFind")
//    public JsonResult finshFind(@ApiParam(value = "订单id") @RequestParam Long transId,
//                            @ApiParam(value = "app类型：1-小程序；2-H5") @RequestParam Integer appType,
//                            HttpServletRequest request) throws WxPayException {
//        ProfitSharingQueryResult result = payService.queryFinish(transId.toString());
//        JsonResult rs = ok();
//        rs.setData(result);
//        return rs;
//    }
//
//    @LoginNeedless
//    @ApiOperation(value = "分账回退")
//    @GetMapping("/refundShare")
//    public JsonResult refundShare(@ApiParam(value = "交易id") @RequestParam String transId,
//                                  HttpServletRequest request) throws WxPayException {
//        ProfitSharingReturnResult receiverResult = payService.refundShare(transId.toString());
//        JsonResult rs = ok();
//        rs.setData(receiverResult);
//        return rs;
//    }
//}
